﻿using System;

namespace zTalk
{
    public class PacketEventArgs : EventArgs
    {
        public object Result { get; set; }
        public PacketEventArgs() { }
        public PacketEventArgs(object result) { Result = result; }
    }

    public delegate void PacketEventHandler(object sender, PacketEventArgs ea);

    public partial class PacketHandlerClient : PacketHandlerBase, IPacketHandler
    {
        public event PacketEventHandler Login;
        public event PacketEventHandler Register;
        public event PacketEventHandler Message;
        public event PacketEventHandler UserOffline;
        public event PacketEventHandler AddFriend;
        public event PacketEventHandler GetFriends;
        public event PacketEventHandler UserChange;
        public event PacketEventHandler UpdateProfile;
        public event PacketEventHandler GetProfile;
        public event PacketEventHandler UpdatePassword;
        public event PacketEventHandler SaveAvatar;
        public event PacketEventHandler FileSend;
        public event PacketEventHandler FileAccept;
        public event PacketEventHandler FileTransfer;
        public event PacketEventHandler GetUser;
        public event PacketEventHandler UserLogout;

        private void onLogin(UserItem user)
        {
            if (Login != null)
            {
                Login(this, new PacketEventArgs(user));
            }
        }

        private void onRegister(bool res)
        {
            if (Register != null)
            {
                Register(this, new PacketEventArgs(res));
            }
        }

        private void onMessage(MessageProto message)
        {
            if (Message != null)
            {
                Message(this, new PacketEventArgs(message));
            }
        }

        private void onUserOffline(string res)
        {
            if (UserOffline != null)
            {
                UserOffline(this, new PacketEventArgs(res));
            }
        }

        private void onAddFriend(UserItem user)
        {
            if (AddFriend != null)
            {
                AddFriend(this, new PacketEventArgs(user));
            }
        }

        private void onGetFriends(UserItem[] users)
        {
            if (GetFriends != null)
            {
                GetFriends(this, new PacketEventArgs(users));
            }
        }

        private void onUserChange(UserItem changes)
        {
            if (UserChange != null)
            {
                UserChange(this, new PacketEventArgs(changes));
            }
        }

        private void onUpdateProfile(string res)
        {
            if (UpdateProfile != null)
            {
                UpdateProfile(this, new PacketEventArgs(res == "ok"));
            }
        }

        private void onGetProfile(string[] res)
        {
            if (GetProfile != null)
            {
                GetProfile(this, new PacketEventArgs(res));
            }
        }

        private void onUpdatePassword(string res)
        {
            if (UpdatePassword != null)
            {
                UpdatePassword(this, new PacketEventArgs(res == "ok"));
            }
        }

        /*private void onGetAvatar(string[] res)
        {
            if (GetAvatar != null)
            {
                GetAvatar(this, new PacketEventArgs(res));
            }
        }*/

        private void onSaveAvatar(string res)
        {
            if (SaveAvatar != null)
            {
                SaveAvatar(this, new PacketEventArgs(res == "ok"));
            }
        }

        private void onFileSend(FileProto res)
        {
            if (FileSend != null)
            {
                FileSend(this, new PacketEventArgs(res));
            }
        }

        private void onFileAccept(FileProto res)
        {
            if (FileAccept != null)
            {
                FileAccept(this, new PacketEventArgs(res));
            }
        }

        private void onFileTransfer(MessageProto message)
        {
            if (FileTransfer != null)
            {
                FileTransfer(this, new PacketEventArgs(message));
            }
        }

        private void onGetUser(UserItem user)
        {
            if (GetUser != null)
            {
                GetUser(this, new PacketEventArgs(user));
            }
        }

        private void onUserLogout(string user)
        {
            if (UserLogout != null)
            {
                UserLogout(this, new PacketEventArgs(user));
            }
        }
    }
}